package com.tengfei.reflection;

import org.junit.Test;

/**
 * @Description: What is refletion
 * @author: ten_fei
 * @date: 2021-07-15 10:15
 */
public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException {
        // 通过反射获取Class对象
        Class<?> c1 = Class.forName("com.tengfei.reflection.User");
        Class<?> c2 = Class.forName("com.tengfei.reflection.User");
        Class<?> c3 = Class.forName("com.tengfei.reflection.User");
        Class<?> c4 = Class.forName("com.tengfei.reflection.User");
        System.out.println(c1);

        // 一个类在内存中只有一个对象
        // 一个类被加载后，整个类的结构都会被封装在CLass对象中
        System.out.println(c1.hashCode());
        System.out.println(c2.hashCode());
        System.out.println(c3.hashCode());
        System.out.println(c4.hashCode());

        System.out.println(User.class);
        System.out.println(new User().getClass());

    }

    @Test
    public void test1() throws ClassNotFoundException {
        Person person=new Student1();
        System.out.println("这个人是："+person.name);

        // 通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());
        // 通过forname()获得
        Class c2 = Class.forName("com.tengfei.reflection.Student1");
        System.out.println(c2.hashCode());

        // 通过.class获得
        Class c3 = Student1.class;
        System.out.println(c3.hashCode());

        // 通过内置类型的包装类都有一个tpye属性
        Class c4 = Integer.TYPE;
        System.out.println(c4.hashCode());

        // 获得父类
        Class c5 = c1.getSuperclass();
        System.out.println(c5);


    }
}

class Person{
    public String name;
    public int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Student1 extends Person{
    public Student1(){
        this.name="feige";
        this.age=18;
    }
}
class Student2 extends Person{
    public Student2(){
        this.name="zhuduiyou";
        this.age=18;
    }
}
















